package com.zxl.grocery.modular.crawler.controller;

import com.zxl.grocery.common.annotion.Permission;
import com.zxl.grocery.common.annotion.log.BussinessLog;
import com.zxl.grocery.common.constant.Const;
import com.zxl.grocery.common.constant.Dict;
import com.zxl.grocery.common.controller.BaseController;
import com.zxl.grocery.common.exception.BizExceptionEnum;
import com.zxl.grocery.common.exception.BussinessException;
import com.zxl.grocery.common.persistence.dao.CrawlerSourcesMapper;
import com.zxl.grocery.common.persistence.model.CrawlerSources;
import com.zxl.grocery.core.log.LogObjectHolder;
import com.zxl.grocery.core.shiro.ShiroKit;
import com.zxl.grocery.core.util.ToolUtil;
import com.zxl.grocery.modular.crawler.dao.CrawlerMgrDao;
import com.zxl.grocery.modular.crawler.warpper.CrawlerMgrWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 爬虫管理控制器
 *
 * @author zxl
 * @Date 2017-07-31 20:46:39
 */
@Controller
@RequestMapping("/crawlerMgr")
public class CrawlerMgrController extends BaseController {

    private String PREFIX = "/crawler/crawlerMgr/";

    @Resource
    private CrawlerMgrDao crawlerMgrDao;

    @Resource
    private CrawlerSourcesMapper crawlerSourcesMapper;
    /**
     * 跳转到爬虫管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "crawlerMgr.html";
    }

    /**
     * 跳转到添加爬虫管理
     */
    @RequestMapping("/crawlerMgr_add")
    public String crawlerMgrAdd() {
        return PREFIX + "crawlerMgr_add.html";
    }

    /**
     * 跳转到修改爬虫管理
     */
    @Permission(Const.ADMIN_NAME)
    @RequestMapping("/crawlerMgr_update/{crawlerMgrId}")
    public String crawlerMgrUpdate(@PathVariable Integer crawlerMgrId, Model model) {
        if (ToolUtil.isEmpty(crawlerMgrId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        //System.out.println(crawlerMgrId);
        CrawlerSources crawlerSources = crawlerSourcesMapper.selectById(crawlerMgrId);
       //System.out.println(crawlerSources.toString());
        model.addAttribute("crawlerSources",crawlerSources);
        LogObjectHolder.me().set(crawlerSources);
        return PREFIX + "crawlerMgr_edit.html";
    }

    /**
     * 获取爬虫管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> result = crawlerMgrDao.getAllCrawlerSourcesList();
        //     page.setRecords((List<CrawlerSources>) new CrawlerSourcesWarpper(result).warp());
        return super.warpObject(new CrawlerMgrWarpper(result));
    }

    /**
     * 新增爬虫管理
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @Permission(Const.ADMIN_NAME)
    @BussinessLog(value = "添加爬虫资源",key = "source", dict = Dict.CrawlerDict)
    @ResponseBody
    public Object add(CrawlerSources crawlerSources) {
        //System.out.println(crawlerSources.toString());
        crawlerSources.setCreateUserId(ShiroKit.getUser().getId());
        crawlerSources.setCreateDate(new Date());
        crawlerSources.insert();
        return SUCCESS_TIP;
    }

    /**
     * 删除爬虫管理
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除爬虫资源", key = "crawlerMgrId", dict = Dict.DeleteDict)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object delete(@RequestParam Integer crawlerMgrId) {
        if (ToolUtil.isEmpty(crawlerMgrId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.crawlerSourcesMapper.deleteById(crawlerMgrId);
        return SUCCESS_TIP;
    }


    /**
     * 修改爬虫管理
     */
    @BussinessLog(value = "修改爬虫资源", key = "source,status,sourceImage", dict = Dict.CrawlerDict)
    @RequestMapping(value = "/update")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object update(CrawlerSources crawlerSources) {
        if (ToolUtil.isEmpty(crawlerSources) || crawlerSources.getId() == null || crawlerSources.getStatus() == null){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        int id = crawlerSources.getId();
        CrawlerSources temp = crawlerSourcesMapper.selectById(id);
        crawlerSources.setCreateDate(temp.getCreateDate());
        crawlerSources.setCreateUserId(temp.getUpdateUserId());
        crawlerSources.setId(temp.getId());
        crawlerSources.setUpdateUserId(ShiroKit.getUser().getId());
        crawlerSources.setModifyDate(new Date());
        crawlerSourcesMapper.updateById(crawlerSources);
        //System.out.println(crawlerSources.toString());
        return super.SUCCESS_TIP;
    }

    /**
     * 爬虫管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
